//
//  StatisticsViewController.m
//  FH2go
//
//  Created by Hannes Feichtl on 12.06.13.
//  Modified by Daniel Hösele
//  Copyright (c) 2014 Daniel Hösele. All rights reserved.
//

#import "StatisticsViewController.h"
#import "StatisticsManager.h"

@implementation StatisticsViewController

@synthesize examid, examname;
@synthesize titleLabel, participants, stats, average;

- (void)viewDidLoad {
    [super viewDidLoad];
        
     /*
     the spinning wheel, which indicates that the marks are loading right now
     */
    UIActivityIndicatorView *loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    if(self.view.center.x > 300)
        [loadingIndicator setCenter:CGPointMake(self.view.center.y+180,self.view.center.x-100)];
    else
        [loadingIndicator setCenter:CGPointMake(self.view.center.x, self.view.center.y)];
    [loadingIndicator startAnimating];
    [self.view addSubview:loadingIndicator];
    
    /*
     shows the marks on the display if no error occured
     */
    StatisticsManager *statisticsManager = [[StatisticsManager alloc] init];
    
    
        
    [statisticsManager fetchStatistics:^(Statistics *statistics, NSError *error){
        [loadingIndicator stopAnimating];
        if (!error) {
            NSString *statString;
            statString = [NSString stringWithFormat: @"    Excellent: %d\n    Good: %d\n    Satisfactory: %d\n    Sufficient: %d\n    Unsatisfactory: %d", statistics.excellent, statistics.good, statistics.satisfactory, statistics.sufficient, statistics.unsatisfactory];
            
            [self.titleLabel setBackgroundColor:[UIColor colorWithRed: 0.9 green: 0.9 blue: 0.9 alpha: 1.0]];
            [self.titleLabel setText: [NSString stringWithFormat: @"   %@", self.examname]];
            
            [self.participants setBackgroundColor:[UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha: 1.0]];
            [self.participants setText: [NSString stringWithFormat: @"    Participants: %d", statistics.participants]];
            
            [self.stats setBackgroundColor:[UIColor colorWithRed: 0.9 green: 0.9 blue: 0.9 alpha: 1.0]];
            [self.stats setText: statString];
            
            [self.average setBackgroundColor:[UIColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha: 1.0]];
            [self.average setText: [NSString stringWithFormat: @"    Average: %@", statistics.average]];
            

        } else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notice" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            
            [alertView show];
        }
    } fromID: self.examid];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
